package com.cloud.tsp.controller;

import com.cloud.core.util.R;
import com.cloud.api.domain.forward.ForwardRequest;
import com.cloud.api.domain.forward.QueryVehiclePlat;
import com.cloud.api.domain.forward.ResponseData;
import com.cloud.api.domain.forward.VehiclePlatform;
import com.cloud.api.validgroup.ValidateGroupInsert;
import com.cloud.api.validgroup.ValidateGroupUpdate;
import com.cloud.api.constants.StrVo;
import com.cloud.log.annotation.Log;
import com.cloud.log.enums.BusinessType;
import com.cloud.log.enums.OperatorType;
import com.cloud.mybatis.domain.TableDataInfo;
import com.cloud.tsp.service.IVehiclePlatformService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author haohaibo
 * @since 2024-01-09
 */
@RestController
@Validated
@RequestMapping("/vehicle-platform")
public class VehiclePlatformController {


    @Resource
    private IVehiclePlatformService vehiclePlatformService;

    @Tag(name = "v1.0")
    @Operation(summary = "车辆平台关系管理（管理）")
    @PostMapping("/getVehiclePlatformList")
    @Log(title = "转发车辆管理-车辆平台关系管理", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public TableDataInfo<VehiclePlatform> selectVehiclePlatformList(@RequestBody @Valid QueryVehiclePlat para){
        return vehiclePlatformService.selectVehiclePlatformList(para);
    }

    @Tag(name = "v1.0")
    @Operation(summary = "新增车辆平台关系（管理）")
    @PostMapping("/InsertVehiclePlatform")
    @Log(title = "转发车辆管理-新增车辆平台关系", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    public R<String> InsertVehiclePlatform(@Validated(ValidateGroupInsert.class) @RequestBody @Valid ForwardRequest para){
        return R.ok(vehiclePlatformService.InsertVehiclePlatform(para));
    }

    @Tag(name = "v1.0")
    @Operation(summary = "开启关闭转发（管理）")
    @PostMapping("/updateStatus")
    @Log(title = "转发车辆管理-开启关闭转发", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    public R updateStatus(@Validated(ValidateGroupUpdate.class) @RequestBody @Valid ForwardRequest para){
        vehiclePlatformService.updateStatus(para);
        return R.ok();
    }

    @Tag(name = "v1.0")
    @Operation(summary = "删除车辆（管理）")
    @PostMapping("/deletedById")
    @Log(title = "转发车辆管理-删除车辆", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    public R deletedById(@RequestBody @Valid StrVo para){
        vehiclePlatformService.deletedById(para.getId());
        return R.ok();
    }

    @Tag(name = "v1.0")
    @Operation(summary = "下载模板")
    @PostMapping("/downloadTemplate")
    @Log(title = "转发车辆管理-下载模板", businessType = BusinessType.EXPORT, operatorType = OperatorType.MANAGE)
    public void downloadTemplate(HttpServletResponse response) throws IOException {
        vehiclePlatformService.downloadTemplate(response);
    }

    @Tag(name = "v1.0")
    @Operation(summary = "导入转发车辆管理")
    @PostMapping("/importVinPlatRelate")
    @Log(title = "转发车辆管理-导入转发车辆管理", businessType = BusinessType.IMPORT, operatorType = OperatorType.MANAGE)
    public R<ResponseData> importVinPlatRelate(@RequestParam MultipartFile file, @NotEmpty String id, HttpServletResponse response) throws IOException {
        String key = "vehicle";
        ResponseData responseData = vehiclePlatformService.importVinPlatRelate(id, file, response, key);
        return R.ok(responseData);
    }

    @Tag(name = "v1.0")
    @Operation(summary = "下载错误文件(管理)")
    @PostMapping("/downloadFile")
    @Log(title = "转发车辆管理-下载错误文件", businessType = BusinessType.EXPORT, operatorType = OperatorType.MANAGE)
    public R downloadFile(@RequestParam String fileName, HttpServletResponse response) throws IOException{
        vehiclePlatformService.downloadFile(fileName,response);
        return R.ok();
    }

}